package com.chujiekeji.test.activity;


import android.app.ProgressDialog;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.chujiekeji.test.BaseApplication;
import com.chujiekeji.test.R;
import com.chujiekeji.test.activity.chat.ChatActivity;
import com.chujiekeji.test.adapter.HomeListAdapter;
import com.chujiekeji.test.api.ChujieApi;
import com.chujiekeji.test.base.BaseActivity;
import com.chujiekeji.test.bean.PageBean;
import com.chujiekeji.test.bean.ResultBean;
import com.chujiekeji.test.bean.Task;
import com.chujiekeji.test.util.AppOperator;
import com.chujiekeji.test.util.DialogHelper;
import com.chujiekeji.test.util.TLog;
import com.chujiekeji.test.util.ToastUtil;
import com.chujiekeji.test.view.PortraitView;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.TextHttpResponseHandler;

import butterknife.BindView;
import butterknife.OnClick;
import cz.msebera.android.httpclient.Header;

public class TaskDetailActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.tv3)
    TextView tv3;
    @BindView(R.id.tv4)
    TextView tv4;
    @BindView(R.id.tv5)
    TextView tv5;
    @BindView(R.id.tv6)
    TextView tv6;
    @BindView(R.id.taskContent)
    TextView taskContent;
    @BindView(R.id.tv_nick)
    TextView nick;
    @BindView(R.id.receiveTask)
    Button receiveTask;

    @BindView(R.id.iv_portrait)
    PortraitView mPortrait;
    private ProgressDialog mDialog;
    private String helpNum;
    TextHttpResponseHandler mHandler;
    private String nickName;
    @Override
    public void initView() {
        setContentView(R.layout.activity_task_detail);
    }
    public ProgressDialog showWaitDialog(int messageId) {
        String message = getResources().getString(messageId);
        if (mDialog == null) {
            mDialog = DialogHelper.getProgressDialog(getActivity(), message);
        }

        mDialog.setMessage(message);
        mDialog.show();

        return mDialog;
    }
    @Override
    public void initData() {
        mHandler= new TextHttpResponseHandler() {
            @Override
            public void onStart() {
                showWaitDialog(R.string.updating);
                super.onStart();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                if (mDialog != null && mDialog.isShowing()) mDialog.dismiss();
                ToastUtil.showShortToast(responseString);
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                try {
                    ResultBean<PageBean<Task>> resultBean = AppOperator.createGson().fromJson(responseString, new TypeToken<ResultBean<PageBean<Task>>>() {
                    }.getType());
                    ToastUtil.showShortToast(resultBean.getMessage());
                } catch (Exception e) {
                    e.printStackTrace();
                    onFailure(statusCode, headers, responseString, e);
                }
            }

            @Override
            public void onFinish() {
                super.onFinish();
                if (mDialog != null && mDialog.isShowing()) mDialog.dismiss();
            }

            @Override
            public void onCancel() {
                super.onCancel();
                if (mDialog != null && mDialog.isShowing()) mDialog.dismiss();
            }
        };
        mPortrait.setImageResource(R.mipmap.widget_default_face);
        Intent intent = getIntent();
        Long id = intent.getLongExtra("taskId",0L);
        String content = intent.getStringExtra("content");
        String nickNameStr = intent.getStringExtra("nickName");
        String helpNumStr = intent.getStringExtra("helpNum");
        nickName=nickNameStr;
        helpNum=helpNumStr;
        String time = intent.getStringExtra("time");
        int reward = intent.getIntExtra("moneyReward",0);
        taskContent.setText(content);
        nick.setText(nickName);
        tv4.setText(time);
        tv5.setText(reward+"");
        TLog.i(this.getClass().getName(),id+"");
    }

    @Override
    public void initEven() {

    }
    @Override
    @OnClick({R.id.tv1,R.id.tv2,R.id.tv3,R.id.receiveTask})
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv1:
                ChujieApi.addGuanzhu(helpNum,mHandler);
                break;
            case R.id.tv2:
                ChujieApi.addFriend(helpNum,mHandler);
                break;
            case R.id.tv3:
                Intent intent = new Intent(this, ChatActivity.class);
                intent.putExtra("nickName",nickName);
                intent.putExtra("helpNum",helpNum);
                startActivity(intent);
                break;
            case R.id.receiveTask:
                ToastUtil.showShortToast("已接受任务");
                break;
        }
    }
}
